package com.example.chongwushangcheng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.Product;

import java.util.List;

public class SimilarProductAdapter extends RecyclerView.Adapter<SimilarProductAdapter.ProductViewHolder> {

    private Context context;
    private List<Product> productList;
    private OnProductClickListener listener;

    public interface OnProductClickListener {
        void onProductClick(Product product);
        void onHeartClick(Product product, int position);
        void onDeleteClick(Product product, int position);
    }

    public SimilarProductAdapter(Context context, List<Product> productList) {
        this.context = context;
        this.productList = productList;
    }

    public void setOnProductClickListener(OnProductClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_similar_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        Product product = productList.get(position);
        
        // 设置商品信息
        holder.tvProductTitle.setText(product.getName());
        holder.tvProductPrice.setText("¥" + product.getPrice());
        holder.tvEndTime.setText("今天20:05截拍"); // 固定文本，实际应该从数据获取
        holder.tvSellerName.setText("招财猫旺财狗"); // 固定文本，实际应该从数据获取
        
        // 设置图片（目前使用默认图片）
        holder.ivProductImage.setImageResource(R.drawable.ic_user_avatar);
        holder.ivSellerAvatar.setImageResource(R.drawable.ic_user_avatar);
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onProductClick(product);
            }
        });
        
        holder.ivHeartIcon.setOnClickListener(v -> {
            if (listener != null) {
                listener.onHeartClick(product, position);
            }
        });
        
        holder.ivDeleteBtn.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteClick(product, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productList != null ? productList.size() : 0;
    }

    public void removeItem(int position) {
        if (position >= 0 && position < productList.size()) {
            productList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, getItemCount());
        }
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView ivProductImage;
        TextView tvQualityTag;
        ImageView ivHeartIcon;
        TextView tvProductTitle;
        TextView tvProductPrice;
        TextView tvGuaranteeTag;
        TextView tvEndTime;
        ImageView ivSellerAvatar;
        TextView tvSellerName;
        ImageView ivDeleteBtn;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            ivProductImage = itemView.findViewById(R.id.ivProductImage);
            tvQualityTag = itemView.findViewById(R.id.tvQualityTag);
            ivHeartIcon = itemView.findViewById(R.id.ivHeartIcon);
            tvProductTitle = itemView.findViewById(R.id.tvProductTitle);
            tvProductPrice = itemView.findViewById(R.id.tvProductPrice);
            tvGuaranteeTag = itemView.findViewById(R.id.tvGuaranteeTag);
            tvEndTime = itemView.findViewById(R.id.tvEndTime);
            ivSellerAvatar = itemView.findViewById(R.id.ivSellerAvatar);
            tvSellerName = itemView.findViewById(R.id.tvSellerName);
            ivDeleteBtn = itemView.findViewById(R.id.ivDeleteBtn);
        }
    }
} 